﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemPhieuDanhGiaNCCForm : Form
    {
        private string _maPhieu;

        public ThemPhieuDanhGiaNCCForm()
        {
            InitializeComponent();
        }

        public ThemPhieuDanhGiaNCCForm(String maPhieuDanhGiaNCC)
        {
            InitializeComponent();
            _maPhieu = maPhieuDanhGiaNCC;
        }

        private void ThemPhieuDanhGiaNCCForm_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            // fill combobox hang hoa
            TenHangHoa.DisplayMember = "TenHangHoa";
            TenHangHoa.ValueMember = "Self";
            var HangHoas = from i in dc.HangHoas where i.deleted == false select i;
            TenHangHoa.DataSource = HangHoas;

            // fill combobox ncc
            TenNhaCungCap.DisplayMember = "TenNhaCungCap";
            TenNhaCungCap.ValueMember = "Self";
            var listNCC = from s in dc.NhaCungCaps where s.deleted == false select s;
            TenNhaCungCap.DataSource = listNCC;

            // if edit, show info
            if (_maPhieu != null)
            {
                _DB.PhieuDanhGiaNCC p = dc.PhieuDanhGiaNCCs.Single(x => x.MaPhieu == _maPhieu);
                txtMaPhieu.Text = p.MaPhieu;
                txtContent.Text = p.Content;

                // fill grid hang hoa
                int count = 1;
                foreach (_DB.HangHoa_NhaCungCap h in p.HangHoa_NhaCungCaps)
                {
                    DataGridViewRow r = new DataGridViewRow();
                    r.CreateCells(dgvHangHoa);
                    r.Cells[0].Value = count++;
                    r.Cells[1].Value = h.HangHoa;
                    r.Cells[2].Value = h.NhaCungCap;
                    r.Cells[3].Value = h.ChatLuong.ToString();
                    r.Cells[4].Value = h.Gia.ToString();
                    r.Cells[5].Value = h.ThoiGianGiaoHang.ToString();
                    r.Cells[6].Value = h.HauMai.ToString();
                    r.Cells[7].Value = h.HinhThucThanhToan.ToString();
                    r.Cells[8].Value = h.Dat;
                    r.Cells[9].Value = h.GhiChu;

                    dgvHangHoa.Rows.Add(r);
                }
            }
            else
                txtMaPhieu.Text = _DB.DinhDangMaPhieu.getMaPhieuMoi(_DB.DinhDangMaPhieu.DINH_DANG_PHIEU_DANH_GIA_NCC_ID);
        }

        private void _DoNothingOnDataError(object sender, DataGridViewDataErrorEventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                // check duplicate
                for (int i = 0; i < dgvHangHoa.Rows.Count - 1; i++)
                for (int j = i + 1; j < dgvHangHoa.Rows.Count; j++)
                {
                    if (dgvHangHoa.Rows[i].Cells[1].Value == dgvHangHoa.Rows[j].Cells[1].Value &&
                        dgvHangHoa.Rows[i].Cells[2].Value == dgvHangHoa.Rows[j].Cells[2].Value)
                    {
                        MessageBox.Show("Có đánh giá trùng lặp", "Error");
                        return;
                    }
                }

                // go
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                _DB.PhieuDanhGiaNCC p;
                if (_maPhieu == null)
                {
                    p = new _DB.PhieuDanhGiaNCC();
                    p.MaPhieu = txtMaPhieu.Text;
                    p.MaNguoiTao = Program.MaNguoiDungHienTai;
                    p.NgayTao = DateTime.Now;
                    p.TrangThai = _DB.TrangThaiPhieu.CHINH_SUA_ID;
                }
                else
                {
                    p = dc.PhieuDanhGiaNCCs.Single(x => x.MaPhieu == _maPhieu);
                    dc.HangHoa_NhaCungCaps.DeleteAllOnSubmit(p.HangHoa_NhaCungCaps);
                }
                
                p.Content = txtContent.Text;
                foreach (DataGridViewRow r in dgvHangHoa.Rows)
                if (!r.IsNewRow)
                {
                    _DB.HangHoa_NhaCungCap HangHoa_NCC = new _DB.HangHoa_NhaCungCap();

                    HangHoa_NCC.MaHangHoa = (r.Cells[1].Value as _DB.HangHoa).MaHangHoa;
                    HangHoa_NCC.MaNhaCungCap = (r.Cells[2].Value as _DB.NhaCungCap).MaNhaCungCap;

                    HangHoa_NCC.ChatLuong = Int32.Parse(r.Cells[3].Value as string);
                    HangHoa_NCC.Gia = Int32.Parse(r.Cells[4].Value as string);
                    HangHoa_NCC.ThoiGianGiaoHang = Int32.Parse(r.Cells[5].Value as string);
                    HangHoa_NCC.HauMai= Int32.Parse(r.Cells[6].Value as string);
                    HangHoa_NCC.HinhThucThanhToan = Int32.Parse(r.Cells[7].Value as string);
                    if (r.Cells[8].Value != null)
                        HangHoa_NCC.Dat = (bool) r.Cells[8].Value;// Boolean.Parse(r.Cells[8].Value as string);
                    HangHoa_NCC.GhiChu = r.Cells[9].Value as string;

                    p.HangHoa_NhaCungCaps.Add(HangHoa_NCC);
                }

                if (_maPhieu == null)
                    dc.PhieuDanhGiaNCCs.InsertOnSubmit(p);
                dc.SubmitChanges();
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            
        }

    }
}
